package com.example.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.util.text.BasicTextEncryptor;

@Slf4j
@ApiModel(value = "配置文件加解密", description = "加解密操作")
public class JasyptUtil {
    @ApiModelProperty(value = "加解密盐值")
    private static final String saltValue = "demo";

    @ApiOperation(value = "加密", notes = "对明文加密")
    public static String encrypt(String value) {
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        encryptor.setPassword(saltValue); // 密钥需与配置文件一致
        return encryptor.encrypt(value);
    }

    @ApiOperation(value = "解密", notes = "对密文解密")
    public static String decrypt(String value) {
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        encryptor.setPassword(saltValue); // 密钥需与配置文件一致
        return encryptor.decrypt(value);
    }

    public static void main(String[] args) {
        // 明文
        String pwd = "123456";
        //密文
        String encrypt = encrypt(pwd);
        log.info("明文[{}]加密后的密文[{}]", pwd, encrypt);
        log.info("密文[{}]解密后的明文[{}]", encrypt, decrypt(encrypt));
    }
}